<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\common\model\jia;

use cores\BaseModel;
use think\model\relation\BelongsTo;
use think\model\relation\HasMany;

/**
 * 商家门店店员模型
 * Class Clerk
 * @package app\common\model\store
 */
class Feedback extends BaseModel
{
    // 定义表名
    protected $name = 'jia_feedback';

    // 定义主键
    protected $pk = 'id';
    // 追加字段
    protected $append = ['status_text'];

    public function getStatusTextAttr($value, $data)
    {
        switch ($data['status']) {
            case 1:
                $status = '已处理';
                break;

            default:
                $status = '未处理';
                break;
        }
        return $status;
    }
    /**
     * 关联用户表
     * @return BelongsTo
     */
    public function user(): BelongsTo
    {
        $model = "app\\common\\model\\User";
        return $this->belongsTo($model)->field(['user_id', 'nick_name', 'avatar_id', 'create_time', 'gender']);
    }
    public function getListAll(array $param = [])
    {
        // 默认查询参数
        $params = $this->setQueryDefaultValue($param, [
            'status' => -1, // 状态(1显示 0隐藏 -1全部)
            'title'  => '',
        ]);
        // 设置检索条件
        $filter                               = [];
        $params['status'] > -1 && $filter[]   = ['status', '=', $params['status']];
        ! empty($params['title']) && $filter[] = ['name|phone', 'like', '%' . $params['title'] . '%'];
        // 查询列表数据
        return $this->with(['images.file'])
            ->where($filter)
            ->where('is_delete', 0)
            ->field('name,phone,content,status,remark')
            ->order(['create_time desc'])
            ->select()->toArray();
    }
    /**
     * 关联评价图片表
     * @return HasMany
     */
    public function images(): HasMany
    {
        $model = "app\\common\\model\\jia\\FeedbackImage";
        return $this->hasMany($model, 'feedback_id', 'id');
    }
    public static function detail($where)
    {
        return static::get($where);
    }
    /**
     * 文章详情：HTML实体转换回普通字符
     * @param $value
     * @return string
     */
    public function getRemarkAttr($value): string
    {
        return $value ? htmlspecialchars_decode($value) : '';
    }
}
